package com.xujiang.common_plus.core;

import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AndroidRuntimeException;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xujiang.common_core.util.ui.UIUtils;
import com.xujiang.common_plus.R;

/**
 * Created by xujiang on 2017/7/15.
 */

public class ToolBarHelper {
    private Toolbar toolbar;
    private TextView toolbarTitle;
    private View toolbarRight;
    private View toolbarLeft;

    public static ToolBarHelper with(@NonNull AppCompatActivity activity){
        Toolbar toolbar = UIUtils.findViewById(activity, R.id.ek_cp_toolbar);
        activity.setSupportActionBar(toolbar);
        ActionBar actionBar = activity.getSupportActionBar();
        if (actionBar == null) {
            throw new AndroidRuntimeException("SupportActionBar is null");
        }
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);  //显示actionBar的回退在toolbar
        return new ToolBarHelper(toolbar);
    }

    public static ToolBarHelper with(@NonNull View view) {
        return new ToolBarHelper((Toolbar) UIUtils.findViewById(view, R.id.ek_cp_toolbar));
    }

    public ToolBarHelper(Toolbar toolbar) {
        if (toolbar == null) {
            throw new IllegalArgumentException("Toolbar is null");
        }
        this.toolbar = toolbar;
        toolbarTitle = (TextView) toolbar.findViewById(R.id.ek_cp_toolbar_title);
        toolbarRight = toolbar.findViewById(R.id.ek_cp_toolbar_right);
        toolbarLeft = toolbar.findViewById(R.id.ek_cp_toolbar_left);
    }

    public void toggleVisibilityToolbarTitle() {  //标题切换显示和隐藏
        UIUtils.toggleVisibility(toolbarTitle);
    }

    public void toggleVisibilityToolbarRight() {  //右图标切换显示和隐藏
        UIUtils.toggleVisibility(toolbarRight);
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    public TextView getToolbarTitle() {
        return toolbarTitle;
    }

    /**
     * def {@link ImageButton}
     */
    public View getToolbarRight() {
        return toolbarRight;
    }

    /**
     * def {@link ImageButton}
     */
    public View getToolbarLeft() {
        return toolbarLeft;
    }

}
